package com.itheima.mall.dubbo.api.ums;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mall.commons.page.PageResult;
import com.itheima.mall.dubbo.mapper.ums.RoleMapper;
import com.itheima.mall.dubbo.mapper.ums.RoleMenuRelationMapper;
import com.itheima.mall.entity.domain.dto.RoleWithMenuDto;
import com.itheima.mall.entity.domain.dto.RoleWithResourceDto;
import com.itheima.mall.entity.domain.ums.*;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 后台用户角色表 服务实现类
 * </p>
 *
 * @author qinglang
 * @since 2022-07-05
 */
@DubboService
@Transactional
public class RoleImplService extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMenuRelationService roleMenuRelationService;

    @Autowired
    private RoleMenuRelationMapper roleMenuRelationMapper;

    @Autowired
    private RoleResourceRelationService roleResourceRelationService;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void allocMenu(RoleWithMenuDto roleWithMenuDto) {
        roleMenuRelationService.remove(new LambdaQueryWrapper<RoleMenuRelation>().eq(Objects.nonNull(roleWithMenuDto.getRoleId()),RoleMenuRelation::getRoleId,roleWithMenuDto.getRoleId()));
        List<RoleMenuRelation> collect = roleWithMenuDto.getMenuIds().stream().map(item -> {
            RoleMenuRelation roleMenuRelation = new RoleMenuRelation();
            roleMenuRelation.setRoleId(roleWithMenuDto.getRoleId());
            roleMenuRelation.setMenuId(item);
            return roleMenuRelation;
        }).collect(Collectors.toList());
        roleMenuRelationService.saveBatch(collect);
    }

    @Override
    public void allocResource(RoleWithResourceDto roleWithResourceDto) {
        roleResourceRelationService.remove(new LambdaQueryWrapper<RoleResourceRelation>().eq(Objects.nonNull(roleWithResourceDto.getRoleId()),RoleResourceRelation::getRoleId,roleWithResourceDto.getRoleId()));
        List<RoleResourceRelation> collect = roleWithResourceDto.getResourceIds().stream().map(item -> {
            RoleResourceRelation roleResourceRelation = new RoleResourceRelation();
            roleResourceRelation.setRoleId(roleWithResourceDto.getRoleId());
            roleResourceRelation.setRoleId(item);
            return roleResourceRelation;
        }).collect(Collectors.toList());
        roleResourceRelationService.saveBatch(collect);
    }

    @Override
    public void create(Role role) {
        this.save(role);
    }

    @Override
    public void delete(List<Integer> ids) {
        this.removeByIds(ids);
    }

    @Override
    public PageResult listR(String keyword, Integer pageNum, Integer pageSize) {
        Page<Role> rolePage = new Page<>(pageNum,pageSize);
        Page<Role> page = this.page(rolePage, new LambdaQueryWrapper<Role>().like(Objects.nonNull(keyword), Role::getName, keyword));
        return PageResult.init(page.getRecords(),page.getCurrent(),page.getSize(),page.getTotal());
    }

    @Override
    public List<Role> listAll() {
        return this.list();
    }

    @Override
    public List<Menu> listMenu(Integer roleId) {
        List<Menu> menus = roleMapper.getMenu(roleId);
        return menus;
    }

    @Override
    public List<Resource> listResource(Integer roleId) {
        List<Resource> resource = roleMapper.getResource(roleId);
        return resource;

    }

    @Override
    public void updateR(Role role) {
        this.updateR(role);
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        Role role = new Role();
        role.setId(id);
        role.setStatus(status);
        this.updateById(role);
    }
}
